package cn.wolfcode.car.business.web.controller;

import cn.wolfcode.car.business.domain.Goods;
import cn.wolfcode.car.business.domain.StockBillItem;
import cn.wolfcode.car.business.query.StockBillItemQuery;
import cn.wolfcode.car.business.service.IGoodsService;
import cn.wolfcode.car.business.service.IStockBillItemService;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.web.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 岗位控制器
 */
@Controller
@RequestMapping("business/stockBillItem")
public class StockBillItemController {
    //模板前缀
    private static final String prefix = "business/stockBillItem/";

    @Autowired
    private IStockBillItemService stockBillItemService;
    @Autowired
    private IGoodsService goodsService;

    //页面------------------------------------------------------------
    //列表
    @RequiresPermissions("business:stockBillItem:view")
    @RequestMapping("/listPage")
    public String listPage(){
        return  "list";
    }

    @RequiresPermissions("business:stockBillItem:add")
    @RequestMapping("/addPage")
    public String addPage(){
        return prefix + "add";
    }


    @RequiresPermissions("business:stockBillItem:edit")
    @RequestMapping("/editPage")
    public String editPage(Long id, Model model){
        model.addAttribute("stockBillItem", stockBillItemService.get(id));
        return prefix + "edit";
    }

    //数据-----------------------------------------------------------
    //列表
    @RequiresPermissions("business:stockBillItem:list")
    @RequestMapping("/query")
    @ResponseBody
    public TablePageInfo<StockBillItem> query(StockBillItemQuery qo){
        return stockBillItemService.query(qo);
    }


    //编辑
    @RequiresPermissions("business:stockBillItem:edit")
    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(StockBillItem stockBillItem){
        stockBillItemService.update(stockBillItem);
        return AjaxResult.success();
    }

    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult addSave(StockBillItem stockBillItem){
        stockBillItemService.save(stockBillItem);
        return AjaxResult.success();
    }

    //删除
    @RequiresPermissions("business:statement:remove")
    @RequestMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String id){
        stockBillItemService.deleteBatch(id);
        return AjaxResult.success();
    }

    // 删除 billId为0的物品
    @RequiresPermissions("business:stockBillItem:remove")
    @RequestMapping("/removeAll")
    @ResponseBody
    public AjaxResult removeAll(Long id){
        stockBillItemService.removeBillItem(id);
        return AjaxResult.success();
    }

    @RequestMapping("/updateAmounts")
    @ResponseBody
    public AjaxResult updateAmounts(@RequestBody List<Goods> list){
        stockBillItemService.updateAmounts(list);
        return AjaxResult.success();
    }

}
